Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

239
Vistas
Why does document.getElementsByClassName("className")[0].style.display return an empty string

I'm trying to create a button that makes an element disappear/reappear depending on whether the display property is equal "block" or "none" respectively. Here's what I tried:

// this is a function that executes when the button is pressed
{
  let a = document.getElementsByClassName("photo");
  // here a[0] returns the <img> that i want to delete/undelete but  
  // a[0].style.display returns an empty string
  if(a[0].style.display == "block") a[0].style.display = "none"; // so this doesnt work
  else if(a[0].style.display == "none") a[0].style.display = "block";
}

I managed to make it work by adding onLoad = "init()" property to the body tag. init() sets the image's display property to "block" when the page loads, as I understand, and that solved the issue but I want to understand why the display property didnt have a value initially.

Please note that I set the display property to "block" manually in css, although I don't think this should matter since img has a default display: inline, so a[0].style.display should return "inline" instead of an empty string.

This was my first question here so if there's something I should've done differently, please feel free to let me know :)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

From what I've read, an empty string can mean that its the element's default. So for div that would be block.

What does style.display = '' actually do?

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda